import { location } from "../utils/getLocation";
import QCSettings from "../utils/QuickCommandSettings";
import { getItemFromGui, getItemUUID, isModLoaded } from "../utils/utils";
import { InstanceStats } from "./dungeon";

class itemLock {
    lockedItems = [];

    constructor() {
        this.load();
        this.init();
    }

    load() {
        if(FileLib.exists("./config/ChatTriggers/modules/BabyzombieAddons/config/itemLock.json")) {
            try {
                this.lockedItems = JSON.parse(FileLib.read("./config/ChatTriggers/modules/BabyzombieAddons/config/itemLock.json"));
            } catch(e) {}
        }
    }
    save() {
        FileLib.write("./config/ChatTriggers/modules/BabyzombieAddons/config/itemLock.json", JSON.stringify(this.lockedItems,null,4), true);
    }

    toggleItem(item) {
        if(this.lockedItems.indexOf(item) === -1) {
            this.lockedItems.push(item);
            World.playSound("note.pling",1,1.5);
        } else {
            this.lockedItems.splice(this.lockedItems.indexOf(item), 1);
            World.playSound("note.pling",1,0.5);
        }
    }

    isItemLocked(item) {
        let id = getItemUUID(item);
        return id && this.lockedItems.indexOf(id) !== -1;
    }

    init() {
        register("gameUnload", () => {this.save();});

        //在锁住的物品上标个标
        const lockImage = Image.fromAsset("lock.png");
        register("renderItemOverlayIntoGui", (item,x,y,event) => {
            if(!QCSettings.itemLock || !location.inSkyblock) return;
            if(!this.isItemLocked(item)) return;
            Renderer.translate(0,0,260);
            lockImage.draw(x+1,y+1,4,4);
        })

        //取消背包里按Q丢出物品
        const keyBindDrop = new KeyBind(Client.getMinecraft().field_71474_y.field_74316_C);
        register("guiKey", (char,key,gui,event) => {
            if(!QCSettings.itemLock || !location.inSkyblock) return;
            if(key != keyBindDrop.getKeyCode()) return;
            let item = getItemFromGui(gui);
            if(!item || !this.isItemLocked(item)) return;
            cancel(event);
            World.playSound("note.bass",1,0.5);
        })
        
        //防卖npc
        register("guiMouseClick", (mx,my,button,gui,event) => {
            if(!QCSettings.itemLock || !location.inSkyblock) return;
            let item = getItemFromGui(gui);
            if(!item || !this.isItemLocked(item)) return;
            if(!item.getLore().some(e => e.removeFormatting() == "Click to sell!")) return;
            cancel(event);
            World.playSound("note.bass",1,0.5);
        })
        register("packetSent", (packet,event) => {
            if(!QCSettings.itemLock || !location.inSkyblock) return;
            let item = packet.func_149546_g();
            if(!item) {
                let slot = packet.func_149544_d();
                if(!slot || slot < 0) return;
                item = Player.getContainer().getStackInSlot(slot);
            } else item = new Item(item);
            if(!item || !this.isItemLocked(item)) return;
            if(!item.getLore().some(e => e.removeFormatting() == "Click to sell!")) return;
            cancel(event);
            World.playSound("note.bass",1,0.5);
        }).setFilteredClass(net.minecraft.network.play.client.C0EPacketClickWindow)

        //防分解
        register("guiMouseClick", (mx,my,button,gui,event) => {
            if(!QCSettings.itemLock || !location.inSkyblock) return;
            let container = Player.getContainer();
            if(!container || container.getName().removeFormatting() != "Salvage Items") return;
            let item = getItemFromGui(gui);
            if(!item) return;
            let slot = gui.getSlotUnderMouse().field_75222_d;
            if(this.isItemLocked(item) && slot >= 54) {
                cancel(event);
                World.playSound("note.bass",1,0.5);
            } else if(item.getName().removeFormatting() == "Salvage Items") {
                if(container.getItems().slice(10,25).filter(e => e != null).some(e => this.isItemLocked(e))) {
                    cancel(event);
                    World.playSound("note.bass",1,0.5);
                }
            }
        })

        //防丢到界面外面
        const neu = isModLoaded("notenoughupdates") ? Java.type("io.github.moulberry.notenoughupdates.NotEnoughUpdates") : undefined;
        register("guiMouseRelease", (mx,my,button,gui,event) => {
            try {
                if(!QCSettings.itemLock || !location.inSkyblock) return;
                let container = Player.getContainer();
                if(!container) return;
                if(isModLoaded("notenoughupdates") && neu.INSTANCE.config.storageGUI.enableStorageGUI3 &&
                    (container.getName().removeFormatting().includes("Ender Chest") || container.getName().removeFormatting().includes(" Backpack"))
                ) return;
                let slot = gui.getSlotUnderMouse();
                if(slot) return;
                let item = Player.getInventory().inventory.func_70445_o();
                if(!item || !this.isItemLocked(new Item(item))) return;
                cancel(event);
                World.playSound("note.bass",1,0.5);
            } catch (error) {}
        })
        
        //取消正常按Q键丢出物品的发包
        let lastItemLocked = 0;
        register("tick", () => {
            if(!QCSettings.itemLock || !location.inSkyblock) return;
            if(location.inDungeon && InstanceStats.started) return;
            if(Player.getHeldItem() && this.isItemLocked(Player.getHeldItem())) lastItemLocked = Date.now();
        })
        register("packetSent", (packet,event) => {
            if(!QCSettings.itemLock || !location.inSkyblock) return;
            if(location.inDungeon && InstanceStats.started) return;
            if(["DROP_ITEM","DROP_ALL_ITEMS"].indexOf(packet.func_180762_c().toString()) !== -1 && lastItemLocked + 100 > Date.now()) {
                cancel(event);
                World.playSound("note.bass",1,0.5);
            }
        }).setFilteredClass(net.minecraft.network.play.client.C07PacketPlayerDigging)
    }
}

export default new itemLock();